package com.qianbao.finance.sync.modules.elasticsearch.controller;


import com.qianbao.finance.sync.common.constant.SystemConstant;
import com.qianbao.finance.sync.common.entity.R;
import com.qianbao.finance.sync.modules.elasticsearch.model.request.InitMappingRequest;
import com.qianbao.finance.sync.modules.elasticsearch.model.response.Response;
import com.qianbao.finance.sync.modules.elasticsearch.service.MappingService;
import com.qianbao.finance.sync.modules.elasticsearch.util.JsonUtil;
import com.qianbao.finance.sync.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by zhaom on 2017/11/16.
 */
@RestController
@RequestMapping("/es/mapping")
public class MappingController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(MappingController.class);

    @Resource
    private MappingService mappingService;

    @Value("${websocket.address}")
    private String websocketAddr;
    /**
     * 在es里边创建index及mapping信息
     * @param request
     * @return
     */
    @RequestMapping("/initMapping")
    public Response initMapping(@Validated InitMappingRequest request){
        logger.debug("request_info: " + JsonUtil.toJson(request));
        Response response = Response.success(mappingService.initMapping(request));
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

    /**
     * 通过配置信息，全库同步数据
     */
    @RequestMapping("/byConfig")
    public String syncConfig(Integer stepSize) {
        logger.debug("request_info: " + stepSize);
        String response = Response.success(mappingService.syncByConfig(stepSize)).toString();
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

    /**
     * 删除配置相关的es中index
     * @return
     */
    @RequestMapping("/removeAll")
    public Response removeAll(){
        logger.info("begin remove all index");
        Response response = Response.success(mappingService.removeAll());
        logger.info("end of remove");
        return response;
    }



    /**
     * 生成配置信息，需要更新配置文件重启
     * @param database
     * @return
     */
    @RequestMapping("/makeMappingStep")
    public String getmappings(@Validated String database){
        String response = Response.success(mappingService.makeMappingStep(database)).toString();
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

    /**
     * go版本的配置文件
     * @param database
     * @return
     */
    @Deprecated
    @RequestMapping("/makeGoConfig")
    public String getGoConfig(@Validated String database){
        String response = Response.success(mappingService.makeGoMappingStep(database)).toString();
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

    @RequestMapping("/viewConfig")
    public R viewConfig(){
        return R.ok().put(SystemConstant.DATA_ROWS, mappingService.listDbEsMapping()).put("websocketaddr", websocketAddr);
    }
}
